package com.zhiwang.planet.widget


import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.widget.TextView
import com.zhiwang.planet.R
import com.zhiwang.planet.util.dp

class TimeLineTextView(context: Context?, attrs: AttributeSet?) : TextView(context, attrs) {


    private val EXAMPLE_TEXT = "星球采矿"
    private var realTextHeight: Int = 0
    var decorationColor: Int = 0

    var headerHeight = 0

    var headerVisible = true
        set(value) {
            field = value
            postInvalidate()
        }

    var bottomExtra = 100

    var circleRadius = 15

    var lineWidth = 0

    var decorationPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    var circleColor = 0

    var textHeight = 0f

    init {


        val ta = context!!.obtainStyledAttributes(attrs, R.styleable.TimeLineTextView)
        decorationColor = ta.getColor(R.styleable.TimeLineTextView_tl_decoration_color, Color.BLACK)
        headerHeight = ta.getDimensionPixelSize(R.styleable.TimeLineTextView_tl_header_height, 0)
        headerVisible = ta.getBoolean(R.styleable.TimeLineTextView_tl_show_header, true)
        bottomExtra = ta.getDimensionPixelSize(R.styleable.TimeLineTextView_tl_bottom_extra_height, 0)
        circleRadius = ta.getDimensionPixelSize(R.styleable.TimeLineTextView_tl_radius, 1)
        lineWidth = ta.getDimensionPixelSize(R.styleable.TimeLineTextView_tl_line_width, 1.dp.toInt())
        circleColor = ta.getColor(R.styleable.TimeLineTextView_tl_circle_color, Color.BLACK)
        ta.recycle()

        decorationPaint.color = decorationColor
        decorationPaint.style = Paint.Style.FILL
    }

    @SuppressLint("DrawAllocation")
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

        val outRect = Rect()
        paint.getTextBounds(EXAMPLE_TEXT, 0, 2, outRect)

        textHeight = (outRect.bottom - outRect.top).toFloat()

        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        realTextHeight = measuredHeight
        setMeasuredDimension(
                measuredWidth,
                (measuredHeight + headerHeight + circleRadius - textHeight / 2f).toInt() + bottomExtra
        )

    }


    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {

        val translationY = headerHeight + circleRadius - textHeight/2f
        val left = circleRadius - lineWidth / 2f

        decorationPaint.color = decorationColor
        if (headerVisible) {
            canvas?.drawRect(
                    RectF(
                            left,
                            0f,
                            circleRadius + lineWidth / 2f,
                            headerHeight.toFloat()
                    ),
                    decorationPaint
            )
        }
        decorationPaint.color = circleColor
        canvas?.drawCircle(
                circleRadius.toFloat(),
                headerHeight + circleRadius.toFloat(),
                circleRadius.toFloat(),
                decorationPaint
        )

        val excludeAnchorLineTextHeight = realTextHeight - textHeight / 2f
        val bottom = headerHeight + circleRadius + excludeAnchorLineTextHeight + bottomExtra
        decorationPaint.color = decorationColor
        //bottom extra
        canvas?.drawRect(
                left,
                (headerHeight + circleRadius).toFloat(),
                left + lineWidth,
                bottom,
                decorationPaint
        )
        canvas?.save()
        canvas?.translate(0f, translationY)
        super.onDraw(canvas)
    }
}